package com.kgc.crowd.distributioncrowddatabaseprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.crowd.distributioncrowdcommon.VO.MemberConfirmInfoVO;
import com.kgc.crowd.distributioncrowdcommon.VO.MemberLauchInfoVO;
import com.kgc.crowd.distributioncrowdcommon.VO.ProjectVO;
import com.kgc.crowd.distributioncrowdcommon.VO.ReturnVO;
import com.kgc.crowd.distributioncrowdcommon.pojo.*;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.*;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.tracing.NullProviderFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    ProjectPOMapper projectPOMapper;
    @Resource
    TypePOMapper typePOMapper;
    @Resource
    TagPOMapper tagPOMapper;

    @Resource
    ProjectItemPicPOMapper projectItemPicPOMapper;
    @Resource
    MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;
    @Resource
    ReturnPOMapper returnPOMapper;
    @Resource
    MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Transactional(readOnly=false, propagation= Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, String memberId) {
        // 1.保存ProjectPO
        ProjectPO projectPO = new ProjectPO();
        BeanUtils.copyProperties(projectVO, projectPO);
        //把用户id写入projectPO
        int id=Integer.parseInt(memberId);
        projectPO.setMemberid(id);
        projectPOMapper.insert(projectPO);
        // 2.获取保存ProjectPO后得到的自增主键
        // 在ProjectPOMapper.xml文件中insert方法对应的标签中设置useGeneratedKeys="true" keyProperty="id"
        Integer projectId = projectPO.getId();
        System.out.println(projectId);
        //3.保存typeIdList
        List<Integer> typeIdList = projectVO.getTypeIdList();
        if(CrowdUtils.collectionEffectiveCheck(typeIdList)) {
            typePOMapper.insertRelationshipBatch(projectId, typeIdList);
        }
        // 4.保存tagIdList
        List<Integer> tagIdList = projectVO.getTagIdList();
        if(CrowdUtils.collectionEffectiveCheck(tagIdList)) {
            tagPOMapper.insertRelationshipBatch(projectId, tagIdList);
        }
        // 5.保存detailPicturePathList
        // ①从VO对象中获取detailPicturePathList
        System.out.println(projectVO.toString());
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        if(CrowdUtils.collectionEffectiveCheck(detailPicturePathList)) {

            // ②创建一个空List集合，用来存储ProjectItemPicPO对象
            List<ProjectItemPicPO> projectItemPicPOList = new ArrayList<>();

            // ③遍历detailPicturePathList
            for (String detailPath : detailPicturePathList) {

                // ④创建projectItemPicPO对象
                ProjectItemPicPO projectItemPicPO = new ProjectItemPicPO();
                projectItemPicPO.setProjectid(projectId);
                projectItemPicPO.setItemPicPath(detailPath);
                projectItemPicPOList.add(projectItemPicPO);
            }
            System.out.println(projectItemPicPOList.toString());
            // ⑤根据projectItemPicPOList执行批量保存
            projectItemPicPOMapper.insertBatch(projectItemPicPOList);
            // 6.保存MemberLaunchInfoPO
            MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
            if(memberLauchInfoVO != null) {
                //每次添加发起人信息时，先删除再添加
                MemberLaunchInfoPOExample example=new MemberLaunchInfoPOExample();
                example.createCriteria().andMemberidEqualTo(Integer.parseInt(memberId));
                memberLaunchInfoPOMapper.deleteByExample(example);

                MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
                BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);
                memberLaunchInfoPO.setMemberid(Integer.parseInt(memberId));
                memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);
            }
            // 7.根据ReturnVO的List保存ReturnPO
            List<ReturnVO> returnVOList = projectVO.getReturnVOList();
            if(CrowdUtils.collectionEffectiveCheck(returnVOList)) {
                List<ReturnPO> returnPOList = new ArrayList<>();
                for (ReturnVO returnVO : returnVOList) {
                    ReturnPO returnPO = new ReturnPO();
                    BeanUtils.copyProperties(returnVO, returnPO);
                    returnPO.setProjectid(projectId);
                    returnPOList.add(returnPO);
                }
                returnPOMapper.insertBatch(returnPOList);
                // 8.保存MemberConfirmInfoPO
                MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();

                if(memberConfirmInfoVO != null) {
                    MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
                    memberConfirmInfoPO.setCardnum(memberConfirmInfoVO.getCardnum());
                    memberConfirmInfoPO.setMemberid(id);
                    memberConfirmInfoPO.setPaynum(memberConfirmInfoVO.getPaynum());
                    memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);
                }
            }
        }
    }

    @Override
    public List<TypePO> selectTypePo() {
        List<TypePO> typePOS = typePOMapper.selectByExample(null);
        return typePOS;
    }

    @Override
    public List<TagPO> selectTagPo() {
        List<TagPO> tagPOS = tagPOMapper.selectByExample(null);
        return tagPOS;
    }

    @Override
    public List<ProjectPO> selectProjectPo() {
        List<ProjectPO> projectPOS = projectPOMapper.selectByExample(null);
        return projectPOS;
    }

    @Override
    public List<ProjectItemPicPO> selectProjectItemPicPO(Integer projectid) {
        ProjectItemPicPOExample example=new ProjectItemPicPOExample();
        example.createCriteria().andProjectidEqualTo(projectid);
        List<ProjectItemPicPO> projectItemPicPOS = projectItemPicPOMapper.selectByExample(example);
        return projectItemPicPOS;
    }

    @Override
    public List<ReturnPO> selectReturnPo() {
        List<ReturnPO> returnPOS = returnPOMapper.selectByExample(null);
        return returnPOS;
    }

    @Override
    public List<ProjectPO> selectById(Integer id) {
        ProjectPOExample example = new ProjectPOExample();
        ProjectPOExample.Criteria criteria = example.createCriteria();
        criteria.andMemberidEqualTo(id);
        return projectPOMapper.selectByExample(example);
    }
    @Override
    public ProjectPO selctProjectById(Integer id) {
        return projectPOMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    @Override
    public Integer UpdateProjectById(ProjectPO projectPO) {
        return projectPOMapper.updateByPrimaryKey(projectPO);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    @Override
    public Integer deleteProjectById(Integer id) {
        return projectPOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ProjectPO> selectProject(String projectName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ProjectPOExample projectPOExample = new ProjectPOExample();
        if(projectName!=null&&projectName!=""){
            projectPOExample.createCriteria().andProjectNameEqualTo(projectName);
        }
        List<ProjectPO> books = projectPOMapper.selectByExample(projectPOExample);
        return new PageInfo<ProjectPO>(books);
    }
}
